/**
 * Created by LinJe on 2015/10/22.
 */
;(function( $, window ) {

    var olistUl = $('.list-content'),
        tapRefresh = $('.js-refresh'),
        oLoadMore = $('.js-loadmore'),
        webUserId = $('#userId').val(),
        pageStep = 1, totalSize = -1;

    //默认加载
    initLoad();

    //头部点击刷新加载
    tapRefresh.on('click', function() {
        initLoad();
    });

    //开启页面滚动到底部自动加载更多商品
    $.addScrollToBottom();
    $(window).on('scrollToBottom', function() {
        pageStep ++;
        var opts = {'insertFunction':'append', 'pageStep':pageStep, 'count':20,'userId':$('#userId').val()};
        if ( webUserId ) opts.userId = webUserId;
        inserProductContent(opts, function( res ) {
            if ( res.scrollEnd ) {
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            } else {
                $.canloadInfo = true;
            }
        });
    });


    //***********公用方法***************
    //初始化加载
    function initLoad() {
        pageStep = 1;
        totalSize = -1;
        oLoadMore.show().removeClass('mtb50').addClass('mtb300').html('<span class="loading"><a><i class="i-05"></i>正在加载更多</a></span>');
        var opts = {
            'insertFunction':'html',
            'pageStep': pageStep,
            'count': 20
        };
        if ( webUserId ) opts.userId = webUserId;
        inserProductContent(opts, function( res ) {
            if ( res.scrollEnd ) {
                //oLoadMore.hide();
                oLoadMore.removeClass('mtb300').addClass('mtb50').html('<span class="loading"><a>已经是最后一条内容</a></span>');
            }
        });
    }

    //插入商品内容
    function inserProductContent( opts, callBack ) {
        if ( totalSize != -1 && totalSize < pageStep ) {
            callBack && callBack({'scrollEnd':true});
            return;
        }
        publishServer.getProductData(opts, function( msg ) {
            if ( msg.code == 200 ) {
                //获取数据成功
                olistUl[opts.insertFunction]( getMoreHtml(msg.detail.dynamicModules) );
                totalSize = msg.detail.totalSize;
                callBack && callBack({'scrollEnd':totalSize<=1});
            } else {
                window.globalLayer.openWarning({'autoClose':true, time: 2000, text:msg.description});
                callBack && callBack();
            }
        });
    }

    //获取多条商品的html
    function getMoreHtml( data ) {
        var totalHtml = '';

        for ( var i = 0, iLen = data.length; i < iLen; i ++ ) {
            totalHtml += getSingelHtml(data[i]);
        }
        return totalHtml;
    }

    //获取单条商品的html
    function getSingelHtml( data ) {
        var sHtml = '';

        sHtml += '<li class="clearfix" _ugcid="'+ data.ugcId +'">';
        sHtml +=    '<div class="user-avatar"><a href="'+ data.userUrl +'"><img src="'+ data.userAvatar +'" width="50" height="50"></a></div>';
        sHtml +=    '<div class="goods-description-comment"> <i class="i-04"></i>';
        sHtml +=        '<div class="description">';
        sHtml +=            '<div class="text-3"><div class="con"><span class="user-name"><a href="'+ data.userUrl +'">'+ data.userName +'</a></span><span class="label">'+ data.typeDescription +'</span></div></div>';
        sHtml +=            (function() {
                                var str = '';
                                if ( data.url ) {
                                    str = '<div class="link"><span>'+ data.urlDescription +'</span><a href="'+ data.url +'" target="_blank" >'+ data.url +'</a></div>';
                                }
                                return str;
                            })();
        sHtml +=            (function() {
            var str = '';
            if ( data.comment ) {
                str =   '<div class="text-2">' +'<p>'+ data.comment +'</p>' +'</div>';
            }
            return str;
        })();
        sHtml +=            '<div class="w-opt"> <span class="time"><a href="'+ data.ugcUrl +'">'+ data.time +'</a></span></div>';
        sHtml +=            '<div class="text-4">';
        sHtml +=                '<div class="con">';
        sHtml +=                    '<div class="img"><div class="center"><img src="'+ data.prodPicUrl +'?imageMogr2/thumbnail/160x160"></div></div>'
        sHtml +=                    '<div class="text">';
        sHtml +=                        '<h2>'+ data.prodTitle +'</h2>';
        sHtml +=                        '<div class="text-right"><span><a href="'+ data.prodUrl +'">进入商品讨论页 &gt;</a></span></div>';
        sHtml +=                    '</div>';
        sHtml +=                '</div>';
        sHtml +=            '</div>';
        sHtml +=        '</div>';
        sHtml +=        '<div class="bottom"></div>';
        sHtml +=    '</div>';
        sHtml += '</li>';

        return sHtml;
    }

})( jQuery, window );